/*
@title: Precise Maze Game
@author: Malte Hansen
@description: a precise, full resolution (160x128) maze game. Press WASD to move. The game consists of 6 levels. Everytime you complete a level, you get one shield (so you can take one spike hit without dying). Be careful of dying, because every death will increment the death counter.
@tags: ['top-down', 'maze', 'precise', 'full resolution']
@addedOn: 2025-10-12
*/

const playerPixel = "p"
const background = "b"
const spikePixel = "s"
const wallPixel = "w"
const goalPixel = "g"

setLegend(
  [goalPixel, bitmap`
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555`],
  [spikePixel, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
  [wallPixel, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
  [playerPixel, bitmap`
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666`],
  [background, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`]
)

const baseMap = map`
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................
................................................................................................................................................................`

const smallestPossibleXValue = 0
const highestPossibleXValue = 159
const smallestPossibleYValue = 0
const highestPossibleYValue = 127

const playerSize = 6

function summonSpike(x, y) {
  for (let i = 0; i < 8; i++) {
    addSprite(x + i, y + i, spikePixel)
    addSprite(x + 7 - i, y + i, spikePixel)
  }
}

function summonWall(x, y) {
  for (let i = 0; i < 8; i++) {
    for (let j = 0; j < 8; j++) {
      addSprite(x + i, y + j, wallPixel)
    }
  }
}

function summonGoal(x, y) {
  for (let i = 0; i < 8; i++) {
    for (let j = 0; j < 8; j++) {
      addSprite(x + i, y + j, goalPixel)
    }
  }
}

function summonPlayer(x, y) {
  for (let i = 7 - playerSize; i <= playerSize; i++) {
    for (let j = 7 - playerSize; j <= playerSize; j++) {
      addSprite(x + i, y + j, playerPixel)
    }
  }
}

function isFrontEmpty(x, y, playerPixels) {

  for (let i = 0; i < playerPixels.length; i++) {
    let newX = playerPixels[i].x + x
    let newY = playerPixels[i].y + y

    //Check if player is in screen boundaries
    if (newX < smallestPossibleXValue ||
      newX > highestPossibleXValue ||
      newY < smallestPossibleYValue ||
      newY > highestPossibleYValue
    ) {
      return false
    }

    let tileContents = getTile(newX, newY)
    //Check for walls
    for (let sprite of tileContents) {
      if (sprite.type == wallPixel) {
        return false
      }
    }

    //Check for Spikes
    for (let sprite of tileContents) {
      if (sprite.type == spikePixel && shields > 0) {
        shields--
        return false
      }
    }
  }

  return true
}

/*
20x16 Matrix

0: Nothing
1: Spike
2: Wall
3: Player
4: Goal

*/

let grids = [
  [
    [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
    [1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0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clearText()

  setMap(baseMap)
  setBackground(background)

  for (let row = 0; row < grids[currLevel].length; row++) {
    for (let col = 0; col < grids[currLevel][row].length; col++) {
      if (grids[currLevel][row][col] == 1) {
        summonSpike(col * 8, row * 8)
      } else if (grids[currLevel][row][col] == 2) {
        summonWall(col * 8, row * 8)
      } else if (grids[currLevel][row][col] == 3) {
        summonPlayer(col * 8, row * 8)
      } else if (grids[currLevel][row][col] == 4) {
        summonGoal(col * 8, row * 8)
      }
    }
  }

  alive = true


  addText("Level " + (currLevel + 1), { x: 6, y: 7, color: color`0` })
  addText("Deaths: " + deaths, { x: 6, y: 8, color: color`0` })
  addText("Shields: " + shields, { x: 6, y: 9, color: color`0` })

  setTimeout(() => {
    clearText()
  }, 1500)
}

onInput("w", () => {
  if (alive == true) {
    let playerPixels = getAll(playerPixel)

    if (isFrontEmpty(0, -1, playerPixels)) {
      for (let i = 0; i < playerPixels.length; i++) {
        playerPixels[i].y -= 1
      }
    }
  }
})

onInput("a", () => {
  if (alive == true) {
    let playerPixels = getAll(playerPixel)

    if (isFrontEmpty(-1, 0, playerPixels)) {
      for (let i = 0; i < playerPixels.length; i++) {
        playerPixels[i].x -= 1
      }
    }
  }
})

onInput("s", () => {
  if (alive == true) {
    let playerPixels = getAll(playerPixel)

    if (isFrontEmpty(0, 1, playerPixels)) {
      for (let i = 0; i < playerPixels.length; i++) {
        playerPixels[i].y += 1
      }
    }
  }
})

onInput("d", () => {
  if (alive == true) {
    let playerPixels = getAll(playerPixel)

    if (isFrontEmpty(1, 0, playerPixels)) {
      for (let i = 0; i < playerPixels.length; i++) {
        playerPixels[i].x += 1
      }
    }
  }
})

onInput("i", () => {
  if (alive == false) {
    initializeGame()
  }
})

afterInput(() => {
  let playerPixels = getAll(playerPixel)
  let spikePixels = getAll(spikePixel)
  let goalPixels = getAll(goalPixel)

  for (p of playerPixels) {
    for (s of spikePixels) {
      if (p.x == s.x && p.y == s.y && alive) {
        deaths++
        if (shields > 0) {
          shields--
        }

        alive = false

        addText("You Died!", { x: 1, y: 6, color: color`0` })
        addText("Deaths: " + deaths, { x: 1, y: 7, color: color`0` })
        addText("Shields: " + shields, { x: 1, y: 8, color: color`0` })
        addText("Press i to respawn", { x: 1, y: 9, color: color`0` })
      }
    }

    for (g of goalPixels) {
      if (p.x == g.x && p.y == g.y && currLevel < grids.length - 1 && alive) {
        currLevel++
        shields++
        alive = false
        initializeGame()
      }
    }
  }
})

let alive = true
let currLevel = 5
let deaths = 0
let shields = 0

initializeGame()
